/**
 * @file modernsnakeentrywidget.cpp
 * @brief 现代化Snake游戏入口界面实现
 */

#include "modernsnakeentrywidget.h"
#include <QApplication>
#include <QScreen>
#include <QMessageBox>
#include <cmath>

// === SnakePreviewWidget 实现 ===

SnakePreviewWidget::SnakePreviewWidget(QWidget *parent)
    : QWidget(parent)
    , m_animationTimer(new QTimer(this))
    , m_animationPhase(0.0f)
    , m_direction(1)
    , m_foodGlow(0.0f)
{
    setFixedSize(400, 300);
    
    // 初始化蛇形路径点 - 更大更流畅的蛇
    for (int i = 0; i < 25; ++i) {
        float angle = i * 0.25f;
        float radius = 80 + 20 * sin(i * 0.1f);
        m_snakePoints.append(QPointF(
            200 + radius * cos(angle),
            150 + radius * sin(angle)
        ));
        
        // 彩虹渐变色彩
        float hue = 0.3f + (float(i) / 25.0f) * 0.2f; // 绿色到青色
        float saturation = 0.8f + 0.2f * sin(i * 0.3f);
        float value = 1.0f - float(i) / 50.0f; // 头部亮，尾部暗
        m_snakeColors.append(QColor::fromHsvF(hue, saturation, value));
    }
    
    // 初始化食物位置
    m_foodPosition = QPointF(300, 100);
    
    // 启动动画定时器
    connect(m_animationTimer, &QTimer::timeout, this, &SnakePreviewWidget::updateAnimation);
    m_animationTimer->start(50);  // 20 FPS
}

void SnakePreviewWidget::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event)
    
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    
    // 绘制背景渐变
    QRadialGradient bgGradient(width()/2, height()/2, width()/2);
    bgGradient.setColorAt(0, QColor(30, 50, 70, 100));
    bgGradient.setColorAt(1, QColor(10, 20, 30, 150));
    painter.fillRect(rect(), bgGradient);
    
    // 绘制蛇身
    for (int i = 0; i < m_snakePoints.size() - 1; ++i) {
        QPointF p1 = m_snakePoints[i];
        QPointF p2 = m_snakePoints[i + 1];
        
        // 计算线条宽度（头部较粗，尾部较细）
        float width = 15.0f * (1.0f - float(i) / m_snakePoints.size());
        
        QPen pen(m_snakeColors[i], width, Qt::SolidLine, Qt::RoundCap);
        painter.setPen(pen);
        painter.drawLine(p1, p2);
    }
    
    // 绘制蛇头
    if (!m_snakePoints.isEmpty()) {
        QPointF head = m_snakePoints.first();
        
        // 蛇头主体
        QRadialGradient headGradient(head, 18);
        headGradient.setColorAt(0, QColor(100, 255, 100));
        headGradient.setColorAt(1, QColor(50, 200, 50));
        painter.setBrush(headGradient);
        painter.setPen(QPen(QColor(30, 150, 30), 3));
        painter.drawEllipse(head, 18, 18);
        
        // 蛇眼睛
        painter.setBrush(Qt::white);
        painter.setPen(Qt::NoPen);
        painter.drawEllipse(head + QPointF(-6, -6), 4, 4);
        painter.drawEllipse(head + QPointF(6, -6), 4, 4);
        
        painter.setBrush(Qt::black);
        painter.drawEllipse(head + QPointF(-5, -5), 2, 2);
        painter.drawEllipse(head + QPointF(7, -5), 2, 2);
    }
    
    // 绘制发光食物
    painter.setPen(Qt::NoPen);
    
    // 外层发光效果
    float glowRadius = 15 + 5 * sin(m_foodGlow);
    QRadialGradient glowGradient(m_foodPosition, glowRadius);
    glowGradient.setColorAt(0, QColor(255, 100, 100, 150));
    glowGradient.setColorAt(1, QColor(255, 100, 100, 0));
    painter.setBrush(glowGradient);
    painter.drawEllipse(m_foodPosition, glowRadius * 2, glowRadius * 2);
    
    // 食物主体
    QRadialGradient foodGradient(m_foodPosition, 8);
    foodGradient.setColorAt(0, QColor(255, 150, 150));
    foodGradient.setColorAt(1, QColor(255, 50, 50));
    painter.setBrush(foodGradient);
    painter.drawEllipse(m_foodPosition, 16, 16);
}

void SnakePreviewWidget::updateAnimation()
{
    m_animationPhase += 0.08f * m_direction;
    m_foodGlow += 0.15f;
    
    // 更新蛇形路径 - 更复杂的运动
    for (int i = 0; i < m_snakePoints.size(); ++i) {
        float angle = i * 0.25f + m_animationPhase;
        float radius = 80 + 20 * sin(m_animationPhase + i * 0.1f) + 10 * cos(m_animationPhase * 0.7f + i * 0.05f);
        
        m_snakePoints[i] = QPointF(
            200 + radius * cos(angle),
            150 + radius * sin(angle) + 15 * sin(m_animationPhase * 0.5f + i * 0.1f)
        );
    }
    
    // 食物位置微调
    m_foodPosition = QPointF(
        300 + 20 * sin(m_animationPhase * 0.3f),
        100 + 15 * cos(m_animationPhase * 0.4f)
    );
    
    // 改变方向
    if (m_animationPhase > 15.0f || m_animationPhase < -15.0f) {
        m_direction *= -1;
    }
    
    update();
}

// === ModernGameModeButton 实现 ===

ModernGameModeButton::ModernGameModeButton(const QString &title, const QString &subtitle, 
                                          const QString &icon, const QColor &color, QWidget *parent)
    : QPushButton(parent)
    , m_title(title)
    , m_subtitle(subtitle)
    , m_icon(icon)
    , m_color(color)
    , m_selected(false)
    , m_hovered(false)
    , m_scaleAnimation(new QPropertyAnimation(this, "geometry"))
    , m_opacityEffect(new QGraphicsOpacityEffect(this))
{
    setFixedSize(280, 120);
    setCursor(Qt::PointingHandCursor);
    
    // 设置透明度效果
    setGraphicsEffect(m_opacityEffect);
    m_opacityEffect->setOpacity(0.9);
    
    // 配置缩放动画
    m_scaleAnimation->setDuration(200);
    m_scaleAnimation->setEasingCurve(QEasingCurve::OutCubic);
}

void ModernGameModeButton::setSelected(bool selected)
{
    if (m_selected != selected) {
        m_selected = selected;
        update();
    }
}

void ModernGameModeButton::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event)
    
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
    
    QRect buttonRect = rect().adjusted(5, 5, -5, -5);
    
    // 计算颜色
    QColor bgColor = m_color;
    if (m_selected) {
        bgColor = bgColor.lighter(140);
    } else if (m_hovered) {
        bgColor = bgColor.lighter(120);
    }
    
    // 绘制背景渐变
    QLinearGradient gradient(buttonRect.topLeft(), buttonRect.bottomRight());
    gradient.setColorAt(0, bgColor);
    gradient.setColorAt(1, bgColor.darker(130));
    
    painter.setBrush(gradient);
    painter.setPen(QPen(bgColor.lighter(150), m_selected ? 3 : 1));
    painter.drawRoundedRect(buttonRect, 15, 15);
    
    // 绘制图标
    painter.setPen(Qt::white);
    QFont iconFont = painter.font();
    iconFont.setPointSize(32);
    iconFont.setBold(true);
    painter.setFont(iconFont);
    
    QRect iconRect(buttonRect.x(), buttonRect.y() + 10, buttonRect.width(), 40);
    painter.drawText(iconRect, Qt::AlignCenter, m_icon);
    
    // 绘制标题
    QFont titleFont = painter.font();
    titleFont.setPointSize(14);
    titleFont.setBold(true);
    painter.setFont(titleFont);
    
    QRect titleRect(buttonRect.x(), buttonRect.y() + 55, buttonRect.width(), 25);
    painter.drawText(titleRect, Qt::AlignCenter, m_title);
    
    // 绘制副标题
    QFont subtitleFont = painter.font();
    subtitleFont.setPointSize(10);
    subtitleFont.setBold(false);
    painter.setFont(subtitleFont);
    painter.setPen(QColor(255, 255, 255, 200));
    
    QRect subtitleRect(buttonRect.x() + 5, buttonRect.y() + 80, buttonRect.width() - 10, 30);
    painter.drawText(subtitleRect, Qt::AlignCenter | Qt::TextWordWrap, m_subtitle);
    
    // 绘制选中指示器
    if (m_selected) {
        painter.setBrush(Qt::white);
        painter.setPen(Qt::NoPen);
        QRect indicator(buttonRect.right() - 20, buttonRect.top() + 10, 10, 10);
        painter.drawEllipse(indicator);
        
        painter.setBrush(m_color);
        painter.drawEllipse(indicator.adjusted(2, 2, -2, -2));
    }
}

void ModernGameModeButton::enterEvent(QEnterEvent *event)
{
    Q_UNUSED(event)
    m_hovered = true;
    m_opacityEffect->setOpacity(1.0);
    update();
}

void ModernGameModeButton::leaveEvent(QEvent *event)
{
    Q_UNUSED(event)
    m_hovered = false;
    m_opacityEffect->setOpacity(0.9);
    update();
}

void ModernGameModeButton::mousePressEvent(QMouseEvent *event)
{
    Q_UNUSED(event)
    click();
}

// === ModernSnakeEntryWidget 主要实现 ===

ModernSnakeEntryWidget::ModernSnakeEntryWidget(QWidget *parent)
    : QWidget(parent)
    , m_titleLabel(nullptr)
    , m_subtitleLabel(nullptr)
    , m_snakePreview(nullptr)
    , m_singlePlayerButton(nullptr)
    , m_multiPlayerButton(nullptr)
    , m_battleRoyaleButton(nullptr)
    , m_backButton(nullptr)
    , m_settingsButton(nullptr)
    , m_playerInfoLabel(nullptr)
    , m_backgroundTimer(new QTimer(this))
    , m_entranceAnimations(nullptr)
    , m_animationTime(0.0f)
{
    setMinimumSize(1200, 800);

    // 设置背景动画
    connect(m_backgroundTimer, &QTimer::timeout, this, &ModernSnakeEntryWidget::updateBackgroundAnimation);
    m_backgroundTimer->start(50);  // 20 FPS

    // 初始化粒子系统
    initializeParticles();

    // 初始化UI
    setupUI();
    setupStyles();
    setupAnimations();

    qDebug() << "ModernSnakeEntryWidget: 现代化Snake游戏入口界面初始化完成";
}

ModernSnakeEntryWidget::~ModernSnakeEntryWidget()
{
    // 析构函数
}

void ModernSnakeEntryWidget::setupUI()
{
    // 创建主布局
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setContentsMargins(0, 0, 0, 0);
    mainLayout->setSpacing(0);

    // === 标题区域 ===
    QWidget *titleArea = new QWidget();
    titleArea->setFixedHeight(120);
    titleArea->setObjectName("titleArea");

    QVBoxLayout *titleLayout = new QVBoxLayout(titleArea);
    titleLayout->setContentsMargins(50, 20, 50, 20);
    titleLayout->setSpacing(10);

    m_titleLabel = new QLabel("🐍 SNAKE ARENA");
    m_titleLabel->setObjectName("mainTitle");
    m_titleLabel->setAlignment(Qt::AlignCenter);

    m_subtitleLabel = new QLabel("现代化贪吃蛇游戏体验");
    m_subtitleLabel->setObjectName("mainSubtitle");
    m_subtitleLabel->setAlignment(Qt::AlignCenter);

    titleLayout->addWidget(m_titleLabel);
    titleLayout->addWidget(m_subtitleLabel);

    // === 中央内容区域 ===
    QWidget *contentArea = new QWidget();
    contentArea->setObjectName("contentArea");

    QHBoxLayout *contentLayout = new QHBoxLayout(contentArea);
    contentLayout->setContentsMargins(50, 30, 50, 30);
    contentLayout->setSpacing(60);

    // 左侧：蛇形预览
    QWidget *leftPanel = new QWidget();
    leftPanel->setObjectName("leftPanel");
    leftPanel->setFixedWidth(450);

    QVBoxLayout *leftLayout = new QVBoxLayout(leftPanel);
    leftLayout->setContentsMargins(20, 20, 20, 20);
    leftLayout->setSpacing(20);

    QLabel *previewTitle = new QLabel("🎮 游戏预览");
    previewTitle->setObjectName("previewTitle");
    previewTitle->setAlignment(Qt::AlignCenter);

    m_snakePreview = new SnakePreviewWidget(this);

    leftLayout->addWidget(previewTitle);
    leftLayout->addWidget(m_snakePreview, 0, Qt::AlignCenter);
    leftLayout->addStretch();

    // 右侧：游戏模式选择
    QWidget *rightPanel = new QWidget();
    rightPanel->setObjectName("rightPanel");

    QVBoxLayout *rightLayout = new QVBoxLayout(rightPanel);
    rightLayout->setContentsMargins(20, 20, 20, 20);
    rightLayout->setSpacing(25);

    QLabel *modeTitle = new QLabel("🎯 选择游戏模式");
    modeTitle->setObjectName("modeTitle");
    modeTitle->setAlignment(Qt::AlignCenter);

    // 创建游戏模式按钮
    m_singlePlayerButton = new ModernGameModeButton(
        "单人模式",
        "经典贪吃蛇体验，练习技巧挑战高分",
        "🎯",
        QColor(76, 175, 80)
    );

    m_multiPlayerButton = new ModernGameModeButton(
        "多人模式",
        "与朋友实时对战，策略制胜",
        "👥",
        QColor(33, 150, 243)
    );

    m_battleRoyaleButton = new ModernGameModeButton(
        "大逃杀模式",
        "生存竞赛，地图缩小，最后王者",
        "⚔️",
        QColor(244, 67, 54)
    );

    // 连接按钮信号
    connect(m_singlePlayerButton, &ModernGameModeButton::clicked,
            this, &ModernSnakeEntryWidget::onSinglePlayerClicked);
    connect(m_multiPlayerButton, &ModernGameModeButton::clicked,
            this, &ModernSnakeEntryWidget::onMultiPlayerClicked);
    connect(m_battleRoyaleButton, &ModernGameModeButton::clicked,
            this, &ModernSnakeEntryWidget::onBattleRoyaleClicked);

    rightLayout->addWidget(modeTitle);
    rightLayout->addWidget(m_singlePlayerButton);
    rightLayout->addWidget(m_multiPlayerButton);
    rightLayout->addWidget(m_battleRoyaleButton);
    rightLayout->addStretch();

    contentLayout->addWidget(leftPanel);
    contentLayout->addWidget(rightPanel);

    // === 底部控制区域 ===
    QWidget *bottomArea = new QWidget();
    bottomArea->setFixedHeight(80);
    bottomArea->setObjectName("bottomArea");

    QHBoxLayout *bottomLayout = new QHBoxLayout(bottomArea);
    bottomLayout->setContentsMargins(30, 15, 30, 15);
    bottomLayout->setSpacing(20);

    m_backButton = new QPushButton("← 返回主菜单");
    m_backButton->setObjectName("backButton");
    m_backButton->setFixedSize(150, 50);

    m_playerInfoLabel = new QLabel("🎮 玩家");
    m_playerInfoLabel->setObjectName("playerInfo");

    m_settingsButton = new QPushButton("⚙️ 设置");
    m_settingsButton->setObjectName("settingsButton");
    m_settingsButton->setFixedSize(100, 50);

    bottomLayout->addWidget(m_backButton);
    bottomLayout->addStretch();
    bottomLayout->addWidget(m_playerInfoLabel);
    bottomLayout->addStretch();
    bottomLayout->addWidget(m_settingsButton);

    // 连接控制按钮信号
    connect(m_backButton, &QPushButton::clicked,
            this, &ModernSnakeEntryWidget::onBackToMenuClicked);
    connect(m_settingsButton, &QPushButton::clicked,
            this, &ModernSnakeEntryWidget::onSettingsClicked);

    // 添加到主布局
    mainLayout->addWidget(titleArea);
    mainLayout->addWidget(contentArea, 1);
    mainLayout->addWidget(bottomArea);
}

void ModernSnakeEntryWidget::setupStyles()
{
    setStyleSheet(R"(
        /* 主窗口样式 */
        ModernSnakeEntryWidget {
            background: transparent;
            color: #ffffff;
            font-family: 'Microsoft YaHei UI', 'Segoe UI', sans-serif;
        }

        /* 标题区域 */
        #titleArea {
            background: rgba(0, 0, 0, 0.1);
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
        }

        /* 主标题 */
        #mainTitle {
            font-size: 56px;
            font-weight: 900;
            color: #ffffff;
            text-shadow: 0 0 30px rgba(76, 175, 80, 1.0),
                         0 0 60px rgba(76, 175, 80, 0.8),
                         0 0 90px rgba(76, 175, 80, 0.6),
                         0 4px 8px rgba(0, 0, 0, 0.5);
            letter-spacing: 8px;
        }

        /* 主副标题 */
        #mainSubtitle {
            font-size: 18px;
            color: rgba(255, 255, 255, 0.9);
            font-weight: 400;
            letter-spacing: 3px;
            text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
        }

        /* 左侧面板 */
        #leftPanel {
            background: rgba(0, 0, 0, 0.15);
            border-radius: 20px;
            border: 1px solid rgba(76, 175, 80, 0.3);
        }

        /* 预览标题 */
        #previewTitle {
            font-size: 22px;
            font-weight: bold;
            color: #4caf50;
            text-shadow: 0 0 10px rgba(76, 175, 80, 0.5);
        }

        /* 右侧面板 */
        #rightPanel {
            background: rgba(0, 0, 0, 0.15);
            border-radius: 20px;
            border: 1px solid rgba(33, 150, 243, 0.3);
        }

        /* 模式标题 */
        #modeTitle {
            font-size: 22px;
            font-weight: bold;
            color: #2196f3;
            text-shadow: 0 0 10px rgba(33, 150, 243, 0.5);
        }

        /* 底部区域 */
        #bottomArea {
            background: rgba(0, 0, 0, 0.2);
            border-top: 1px solid rgba(255, 255, 255, 0.1);
        }

        /* 返回按钮 */
        #backButton {
            background: qlineargradient(x1:0, y1:0, x2:1, y2:1,
                stop:0 rgba(244, 67, 54, 0.8), stop:1 rgba(211, 47, 47, 0.8));
            border: none;
            border-radius: 25px;
            color: white;
            font-size: 14px;
            font-weight: bold;
            text-shadow: 0 1px 3px rgba(0, 0, 0, 0.5);
        }

        #backButton:hover {
            background: qlineargradient(x1:0, y1:0, x2:1, y2:1,
                stop:0 rgba(244, 67, 54, 1.0), stop:1 rgba(211, 47, 47, 1.0));
            transform: scale(1.05);
        }

        /* 设置按钮 */
        #settingsButton {
            background: qlineargradient(x1:0, y1:0, x2:1, y2:1,
                stop:0 rgba(96, 125, 139, 0.8), stop:1 rgba(69, 90, 100, 0.8));
            border: none;
            border-radius: 25px;
            color: white;
            font-size: 14px;
            font-weight: bold;
            text-shadow: 0 1px 3px rgba(0, 0, 0, 0.5);
        }

        #settingsButton:hover {
            background: qlineargradient(x1:0, y1:0, x2:1, y2:1,
                stop:0 rgba(96, 125, 139, 1.0), stop:1 rgba(69, 90, 100, 1.0));
        }

        /* 玩家信息 */
        #playerInfo {
            font-size: 16px;
            font-weight: bold;
            color: #ffffff;
            background: rgba(0, 0, 0, 0.3);
            padding: 10px 20px;
            border-radius: 25px;
            border: 1px solid rgba(255, 255, 255, 0.2);
        }
    )");
}

void ModernSnakeEntryWidget::setupAnimations()
{
    // 创建入场动画序列
    m_entranceAnimations = new QSequentialAnimationGroup(this);

    // 标题淡入动画
    QGraphicsOpacityEffect *titleEffect = new QGraphicsOpacityEffect();
    m_titleLabel->setGraphicsEffect(titleEffect);
    titleEffect->setOpacity(0.0);

    QPropertyAnimation *titleAnim = new QPropertyAnimation(titleEffect, "opacity");
    titleAnim->setDuration(1000);
    titleAnim->setStartValue(0.0);
    titleAnim->setEndValue(1.0);
    titleAnim->setEasingCurve(QEasingCurve::OutCubic);

    m_entranceAnimations->addAnimation(titleAnim);
}

void ModernSnakeEntryWidget::initializeParticles()
{
    // 初始化背景粒子系统
    for (int i = 0; i < 100; ++i) {
        m_particlePositions.append(QPointF(
            QRandomGenerator::global()->bounded(width()),
            QRandomGenerator::global()->bounded(height())
        ));

        m_particleVelocities.append(QPointF(
            (QRandomGenerator::global()->bounded(200) - 100) / 200.0f,
            (QRandomGenerator::global()->bounded(200) - 100) / 200.0f
        ));

        // 蛇主题色彩
        QColor colors[] = {
            QColor(76, 175, 80, 80),    // 绿色
            QColor(139, 195, 74, 70),   // 浅绿
            QColor(255, 193, 7, 90),    // 黄色
            QColor(33, 150, 243, 75),   // 蓝色
            QColor(244, 67, 54, 85),    // 红色
            QColor(156, 39, 176, 70)    // 紫色
        };
        m_particleColors.append(colors[i % 6]);

        m_particleSizes.append(1.0f + QRandomGenerator::global()->bounded(300) / 100.0f);
    }
}

// === 事件处理方法 ===

void ModernSnakeEntryWidget::onSinglePlayerClicked()
{
    qDebug() << "ModernSnakeEntryWidget: 选择单人模式";

    // 设置选中状态
    m_singlePlayerButton->setSelected(true);
    m_multiPlayerButton->setSelected(false);
    m_battleRoyaleButton->setSelected(false);

    // 延迟发送信号，让用户看到选中效果
    QTimer::singleShot(300, this, [this]() {
        emit gameModeSelected("single");
    });
}

void ModernSnakeEntryWidget::onMultiPlayerClicked()
{
    qDebug() << "ModernSnakeEntryWidget: 选择多人模式";

    // 设置选中状态
    m_singlePlayerButton->setSelected(false);
    m_multiPlayerButton->setSelected(true);
    m_battleRoyaleButton->setSelected(false);

    QTimer::singleShot(300, this, [this]() {
        emit gameModeSelected("multiplayer");
    });
}

void ModernSnakeEntryWidget::onBattleRoyaleClicked()
{
    qDebug() << "ModernSnakeEntryWidget: 选择大逃杀模式";

    // 设置选中状态
    m_singlePlayerButton->setSelected(false);
    m_multiPlayerButton->setSelected(false);
    m_battleRoyaleButton->setSelected(true);

    QTimer::singleShot(300, this, [this]() {
        emit gameModeSelected("battle_royale");
    });
}

void ModernSnakeEntryWidget::onBackToMenuClicked()
{
    qDebug() << "ModernSnakeEntryWidget: 返回主菜单";
    emit backToMainMenu();
}

void ModernSnakeEntryWidget::onSettingsClicked()
{
    qDebug() << "ModernSnakeEntryWidget: 打开设置";
    emit settingsClicked();
}

void ModernSnakeEntryWidget::setPlayerInfo(const QString &playerId, const QString &playerName)
{
    m_playerId = playerId;
    m_playerName = playerName;

    m_playerInfoLabel->setText(QString("🎮 %1").arg(playerName));

    qDebug() << "ModernSnakeEntryWidget: 设置玩家信息";
    qDebug() << "  playerId:" << playerId;
    qDebug() << "  playerName:" << playerName;
}

void ModernSnakeEntryWidget::updateBackgroundAnimation()
{
    m_animationTime += 0.05f;

    // 更新粒子位置
    for (int i = 0; i < m_particlePositions.size(); ++i) {
        m_particlePositions[i] += m_particleVelocities[i];

        // 边界检测和反弹
        if (m_particlePositions[i].x() < 0 || m_particlePositions[i].x() > width()) {
            m_particleVelocities[i].setX(-m_particleVelocities[i].x());
        }
        if (m_particlePositions[i].y() < 0 || m_particlePositions[i].y() > height()) {
            m_particleVelocities[i].setY(-m_particleVelocities[i].y());
        }

        // 确保粒子在边界内
        m_particlePositions[i].setX(qBound(0.0, m_particlePositions[i].x(), double(width())));
        m_particlePositions[i].setY(qBound(0.0, m_particlePositions[i].y(), double(height())));
    }

    update();
}

void ModernSnakeEntryWidget::startEntranceAnimations()
{
    if (m_entranceAnimations) {
        m_entranceAnimations->start();
    }
}

void ModernSnakeEntryWidget::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event)

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);

    // 绘制渐变背景
    QLinearGradient bgGradient(0, 0, width(), height());
    bgGradient.setColorAt(0, QColor(15, 25, 45));       // 深蓝紫
    bgGradient.setColorAt(0.3, QColor(25, 35, 65));     // 中蓝
    bgGradient.setColorAt(0.7, QColor(20, 30, 55));     // 深蓝
    bgGradient.setColorAt(1, QColor(10, 20, 40));       // 最深蓝

    painter.fillRect(rect(), bgGradient);

    // 绘制背景效果
    drawBackgroundEffects(painter);
}

void ModernSnakeEntryWidget::drawBackgroundEffects(QPainter &painter)
{
    // 绘制动态粒子
    painter.setPen(Qt::NoPen);
    for (int i = 0; i < m_particlePositions.size(); ++i) {
        QPointF pos = m_particlePositions[i];
        QColor color = m_particleColors[i];
        float size = m_particleSizes[i];

        // 动态透明度
        float alpha = 0.3f + 0.4f * sin(m_animationTime + i * 0.1f);
        alpha = qBound(0.0f, alpha, 1.0f);
        color.setAlphaF(alpha);

        painter.setBrush(color);
        painter.drawEllipse(pos, size, size);
    }

    // 绘制装饰性波浪线
    painter.setPen(QPen(QColor(255, 255, 255, 20), 2, Qt::SolidLine, Qt::RoundCap));

    for (int i = 0; i < 5; ++i) {
        QPainterPath wave;
        float yOffset = height() * (0.2f + i * 0.15f);
        float phase = m_animationTime + i * 0.5f;

        wave.moveTo(0, yOffset);
        for (int x = 0; x <= width(); x += 20) {
            float y = yOffset + 30 * sin(phase + x * 0.01f);
            wave.lineTo(x, y);
        }

        painter.drawPath(wave);
    }
}

void ModernSnakeEntryWidget::resizeEvent(QResizeEvent *event)
{
    QWidget::resizeEvent(event);

    // 重新初始化粒子位置
    if (!m_particlePositions.isEmpty()) {
        for (int i = 0; i < m_particlePositions.size(); ++i) {
            if (m_particlePositions[i].x() > width() || m_particlePositions[i].y() > height()) {
                m_particlePositions[i] = QPointF(
                    QRandomGenerator::global()->bounded(width()),
                    QRandomGenerator::global()->bounded(height())
                );
            }
        }
    }
}
